package com.nxy.cpfsfe.busi.bolag;

import com.adtec.starring.datapool.CompSDO;
import com.adtec.starring.datapool.EPOper;
import com.adtec.starring.exception.BaseException;
import com.adtec.starring.struct.dta.DtaInfo;
import com.nxy.cpfsfe.PubBeanSerial;
import com.nxy.cpfsfe.enumtype.RespCd;
import com.nxy.cpfsfe.enumtype.TcSequenceEnum;
import com.nxy.cpfsfe.service.CpfsService;
import com.nxy.cpfsfe.util.ResponseUtil;
import com.nxy.cpfsfe.util.XmlUtil;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Map;

public class BOlAgGwPSrv implements CpfsService{
	private final static Logger LOGGER = LoggerFactory.getLogger(BOlAgGwPSrv.class);	

	 /**
     * 支付流水交易
     */
	@Override
	public void doService() {
		LOGGER.debug("---------支付流水业务开始---------");
		Map<String,String> map = null;
		String tpId = DtaInfo.getInstance().getTpId();
		// 获取输入
		CompSDO inputSdo = (CompSDO) EPOper.getCompSDO(tpId, "ReqBody"); // 获取输入接口
		// 获取输出
		CompSDO outputSdo = EPOper.getCompSDO(tpId, "RspBody"); // 获取输出接口
		try {
			EPOper.put(tpId, "RspBean[0].head[0].rspsCd", RespCd.NCPS7001);
			EPOper.put(tpId, "RspBean[0].head[0].respsInfoDesc", "流水信息交易失败");
			String bizMsg = (String) inputSdo.getValue("BizMsg")!=null?(String) inputSdo.getValue("BizMsg"):""; // 业务报文
			LOGGER.info("业务报文：[{}]",bizMsg);
			//生成业务map
			map = XmlUtil.xml2Map(bizMsg);
			String currentValue = null;
			PubBeanSerial pubBeanSerial = PubBeanSerial.getInstance();
			currentValue = pubBeanSerial.getSeqNoAndIncrea(TcSequenceEnum.CPFSSEQ.getCode());
			LOGGER.debug("currentValue报文：[{}]",bizMsg);
			if(!StringUtils.isNotEmpty(currentValue)){
				//构建默认返回
				map.put("msg", "流水号获取失败");
				map.put("resultCode",RespCd.NCPO6047);
				return ;
			}
			map.put("currentValue", currentValue);
			//插入数据支付流水表TsPCmd
			pubBeanSerial.insertBySerivalMap(map);
			//返回支付流水信息
			EPOper.put(tpId, "RspBean[0].head[0].rspsCd", RespCd.NCPI0000);
			EPOper.put(tpId, "RspBean[0].head[0].respsInfoDesc", "成功");
			map.put("resultCode",RespCd.NCPI0000);
			map.put("msg", "成功");
			map.put("Reserved", currentValue);
		} catch (BaseException e) {
			LOGGER.error("支付流水业务处理失败",e);
			map.put("msg", e.getErrorDesc());
			map.put("resultCode",e.getErrorCode());
		}catch (Exception e) {
			LOGGER.error("插入数据失败：支付流水表TsPCmd",e);
			map.put("msg", "插入数据失败：支付流水表TsPCmd");
			map.put("resultCode",RespCd.NCPO3999);
		}finally{
			String bizMsgResult = ResponseUtil.buildXmlGeneral(map);
			ResponseUtil.responseSend(bizMsgResult,map);
		}
		LOGGER.debug("---------支付流水业务处理完成---------");
	}

}
